<!--
 * @description:
 * @author: cuicui
 * @Date: 2023-11-28 19:51:46
 * @LastEditTime: 2023-11-30 11:47:52
 * @LastEditors: cuicui
-->
<template>
  <a-modal
    title='新建巡检管理'
    :width='640'
    :visible='visible'
    :confirmLoading='loading'
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }"
  >
    <div style='max-height: 500px;overflow: auto'>
      <a-spin :spinning='loading'>
        <a-form :form='form' v-bind='formLayout'>
          <a-form-item label='巡检名称'>
            <a-input placeholder='请输入巡检名称' v-decorator='["name",{rules:  [
              { required: true, message: "请输入巡检名称" },
                        { pattern: namePattern,  message: "请输入合法内容(可输入0到50位（中文、字母，数字，下划线，减号)"}]}]' />
          </a-form-item>

          <a-form-item label='巡检内容'>
            <a-input placeholder='巡检内容'
                     v-decorator='["content",{rules: [{ required: true, message: "请输入巡检内容" }], validateTrigger: "blur"}]' />
          </a-form-item>

          <a-form-item label='巡检人'>
            <a-input placeholder='请输入巡检人'
                     v-decorator='["inspectionPerson", {rules: [{ required: !model, message: "请输入巡检人" },
              { pattern: userNamePattern,  message: "请输入合法巡检人(2到16位中文、字母,数字,下划线,减号)"}], validateTrigger: "blur"}]' />
          </a-form-item>

          <a-form-item label='巡检时间'>
            <a-date-picker style='width: 100%;' placeholder='请选择巡检时间' v-decorator="['inspectionTime',{
              rules:[ { required: true, message: '请选择巡检时间!' }]
            }]" valueFormat='YYYY-MM-DD' />
          </a-form-item>
          <a-form-item label='巡检人电话'>
            <a-input placeholder='请输入巡检人电话'
                     v-decorator='["phone",{rules: [{ required: true, message: "请输入巡检人电话" }, { pattern: phonePattern,  message: "请输入合法电话"}], validateTrigger: "blur"}]' />
          </a-form-item>
        </a-form>
      </a-spin>
    </div>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { strPattern1 as userNamePattern, phonePattern, namePattern } from '@/reges'
import { findDataDicInfoByType } from '@/api/dataDicInfo'
import { findBypageEquipment } from '@/api/equipmentManagement'
import api, { downloadAvatar } from '@/api/workerOrder'
import TagSelectOption from '@/components/TagSelect/TagSelectOption'
import { mapState } from 'vuex'
import { ACCESS_TOKEN } from '@/store/mutation-types'
// 表单字段
const fields = ['name', 'content', 'inspectionTime', 'inspectionPerson', 'phone']
export default {
  components: { TagSelectOption },
  props: {
    visible: {
      type: Boolean,
      required: true
    },

    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    }
  },
  computed: {
    ...mapState({
      token: state => state.user.token
    }),
    computedHeaders() {
      return {
        [ACCESS_TOKEN]: `Bearer ${this.token}`
      }
    }
  },
  data() {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      }
    }
    return {
      namePattern,
      form: this.$form.createForm(this),
      queryParam: {},
      phonePattern,
      userNamePattern,
      defectLevelDicList: [],
      stationList: [],
      deviceList: [],
      systemId: '',
      imageUrl: '',
      uploadLoading: false
    }
  },
  created() {
    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))
    // this.getLeavelDic()
    // this.getStationList()
    // this.getDeviceList()
    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  methods: {

    getStationList() {
      findDataDicInfoByType(13).then(res => {
        this.stationList = res.map(item => {
          return {
            ...item,
            key: item.code,
            label: item.name
          }
        })
      })
    },
    getDeviceList() {
      findBypageEquipment({ pageNum: 1, pageSize: 999 }).then(res => {
        this.deviceList = res.list.map(item => {
          return {
            ...item,
            label: item.equipmentName,
            key: item.id
          }
        })
      })
    },
    handleChange(info) {
      if (info.file.status === 'uploading') {
        this.uploadLoading = true
        return
      }
      if (info.file.status === 'done') {
        this.imageUrl = downloadAvatar({ fileName: info.file.response.result })
        this.uploadLoading = false
      }
    },
    beforeUpload(file) {
      const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/JEPG'
      if (!isJpgOrPng) {
        this.$message.error('仅支持JPEG格式以及PNG格式文件!')
      }
      const isLt2M = file.size / 1024 / 1024 < 10
      if (!isLt2M) {
        this.$message.error('图片大小不得超过 10MB!')
      }
      return isJpgOrPng && isLt2M
    },
    normFile(e) {
      if (Array.isArray(e)) {
        return e
      }
      if (e.fileList && e.fileList.length) {
        return e && e.fileList[0].response && e.fileList[0].response.result
      }
      return ''
    }
  }
}
</script>
<style lang='less' scoped>
.ant-modal-body {
  height: 700px;
  overflow: auto;
}
</style>
